﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MemoryFormDeal
{
    public struct ProgramSave//进程项目信息定义
    {
        public int No;//标号
        public String PName;//进程名
        public int PMUse;//进程内存使用
        public String colour;//进程标识颜色
        public int pType;//进程分配方式
        public int startunit;//进程开始进程块号
        public int finishunit;//进程结束进程块号
    }
    public struct UnitSave//内存单元定义
    {
        public String colour;//本块显示颜色
        public String belongto;//本块所属进程
    }

    class MemoryDo
    {
        
        private static int Memory = 256;//总内存
        private static int Unit = 2;//每个单元块内存大小
        private int UnitDrawNum = -1;//这里是每一边的绘制数量
        private int UnitRealNum = -1;//实际内存块数量
        private int UnitUsed = 0;//使用中的内存块数量
        private int LastAdd = 0;//最后使用的内存块标号
        private List<ProgramSave> psave = new List<ProgramSave>();//进程链
        private List<UnitSave> usave = new List<UnitSave>();//单元链

        public MemoryDo(){
            CalUnitNum();
            UnitLInit();
        }
        ~MemoryDo(){
        }
        //基本数据操作
        public int GetMemorysize()
        {
            return Memory;
        }
        public int GetUnitsize()
        {
            return Unit;
        }
        public int GetUnitDrawNum()
        {
            return UnitDrawNum;
        }
        public int GetUnitRealNum()
        {
            return UnitRealNum;
        }
        public int GetUnitleftNum()
        {
            return Memory-UnitUsed;
        }
        public bool IsPEmpty()
        {
            return (psave.Count == 0);
        }
        public void SetMemorysize(int memory)
        {
            Memory = memory;
            CalUnitNum();
        }
        public void SetUnitsize(int unit)
        {
            Unit = unit;
            CalUnitNum();
        }
        public List<ProgramSave> getpsave()
        {
            return psave;
        }
        private void CalUnitNum()
        {
            UnitRealNum = Memory / Unit;
            for (int i = 1; ;)
            {
                if ((i * i) >= UnitRealNum)
                {
                    UnitDrawNum = i;
                    break;
                }
                else
                    i++;
            }
        }
        private void UnitLInit()
        {
            UnitSave temp1 = new UnitSave();
            UnitSave temp2 = new UnitSave();
            temp1.colour = "White";
            temp1.belongto = "空闲";
            temp2.colour = "Black";
            temp2.belongto = "无";
            for (int i = 1; i <= (UnitDrawNum * UnitDrawNum); i++)
            {
                if (i <= UnitRealNum)
                    usave.Add(temp1);
                else
                    usave.Add(temp2);
            }
        }
        //对进程存储链操作

        private ProgramSave CompareTo(ProgramSave a,ProgramSave b)
        {
            return (a.startunit < b.startunit) ? a : b;
        }
        public bool AddNewP(ProgramSave pnew)
        {
            switch (pnew.pType)
            {
                case 0:
                    if (FirstFit(pnew))
                        return true;
                    return false;              
                case 1:
                    if (NextFit(pnew))
                        return true;
                    return false; 
                default:
                    return false;
            }
        }
        public bool DelP(ProgramSave pdel)
        {
            int startlocation = pdel.startunit;
            int Id = pdel.No;
            int index=psave.FindIndex(s=>s.startunit==startlocation&&s.No==Id);
            if (index == -1)
                return false;
            else
            {
                UnitSave temp1 = new UnitSave();
                temp1.colour = "White";
                temp1.belongto = "空闲";
                for (int i = psave[index].startunit; i < psave[index].finishunit; i++)
                    usave[i] = temp1;
                psave.RemoveAt(index);
                return true;
            }         
        }
        public void ClaarAll()
        {
            psave.Clear();
            UnitSave temp1 = new UnitSave();
            UnitSave temp2 = new UnitSave();
            temp1.colour = "White";
            temp1.belongto = "空闲";
            temp2.colour = "Black";
            temp2.belongto = "无";
            for (int i = 0; i < (UnitDrawNum * UnitDrawNum); i++)
            {
                if (i < UnitRealNum)
                    usave[i] = temp1;
                else
                    usave[i] = temp2;
            }

        }
        private bool FirstFit(ProgramSave pnew)
        {
            int temp = pnew.PMUse / Unit;//计算本次单元需求(小数点则+1)
            if (pnew.PMUse % Unit != 0)
                temp++;
            UnitSave utemp = new UnitSave();
            ProgramSave ptemp = new ProgramSave();
            
            if (psave.Count==0)//空内存
            {
                if (pnew.PMUse > Memory)//空了都大于最大内存
                    return false;
                else//足够
                {
                    //设置本次节点属性
                    ptemp = pnew;
                    ptemp.startunit = 0;
                    ptemp.finishunit = temp;
                    LastAdd = temp;//设置lastadd为本次操作结尾

                    //设置单元链属性
                    utemp.colour = pnew.colour;
                    utemp.belongto = pnew.PName;
                    for(int i=ptemp.startunit;i<ptemp.finishunit;i++)
                    {
                        usave[i] = utemp;
                    }
                    //添加进程入链表(最后不需要排序)
                    psave.Add(ptemp);
                    return true;
                }
            }
            else if(psave.Count==1)//只有一个进程
            {
                if(psave[0].startunit!=0)//说明这个进程不是从头开始
                {
                    if(psave[0].startunit>=temp)//从头开始到这个内存足够
                    {
                        //设置本次节点属性
                        ptemp = pnew;
                        ptemp.startunit = 0;
                        ptemp.finishunit = temp;
                        LastAdd = (ptemp.finishunit == psave[0].startunit) ? LastAdd : ptemp.finishunit;//这次操作结束如果还有空间则lastadd改变值

                        //设置单元链属性
                        utemp.colour = pnew.colour;
                        utemp.belongto = pnew.PName;
                        for (int i = ptemp.startunit; i < ptemp.finishunit; i++)
                        {
                            usave[i] = utemp;
                        }
                        //添加进程入链表并排序
                        psave.Add(ptemp);
                        psave.Sort((a, b) => a.startunit.CompareTo(b.startunit));
                        return true;
                    }
                    else//如果不够
                    {
                        if (UnitRealNum - psave[0].finishunit < temp)//唯一进程末尾单元和最大单元数量都不够
                            return false;
                        else//足够
                        {
                            //设置本次节点属性
                            ptemp = pnew;
                            ptemp.startunit = psave[0].finishunit;
                            ptemp.finishunit = psave[0].finishunit + temp;
                            LastAdd = ptemp.finishunit;//设置lastadd为本次操作结尾

                            //设置单元链属性
                            utemp.colour = pnew.colour;
                            utemp.belongto = pnew.PName;
                            for (int i = ptemp.startunit; i < ptemp.finishunit; i++)
                            {
                                usave[i] = utemp;
                            }
                            //添加进程入链表(最后不需要排序)
                            psave.Add(ptemp);
                            return true;
                        }
                    }
                }
                if (UnitRealNum - psave[0].finishunit < temp)//唯一进程末尾单元和最大单元数量都不够
                    return false;
                else//足够
                {
                    //设置本次节点属性
                    ptemp = pnew;
                    ptemp.startunit = psave[0].finishunit;
                    ptemp.finishunit = psave[0].finishunit + temp;
                    LastAdd = ptemp.finishunit;//设置lastadd为本次操作结尾

                    //设置单元链属性
                    utemp.colour = pnew.colour;
                    utemp.belongto = pnew.PName;
                    for(int i = ptemp.startunit; i < ptemp.finishunit; i++)
                    {
                        usave[i] = utemp;
                    }
                    //添加进程入链表(最后不需要排序)
                    psave.Add(ptemp);
                    return true;
                }

            }
            else//大于一个进程情况
            {
                if (psave[0].startunit != 0)//说明这个进程不是从头开始
                {
                    if (psave[0].startunit >= temp)//从头开始到这个内存足够
                    {
                        //设置本次节点属性
                        ptemp = pnew;
                        ptemp.startunit = 0;
                        ptemp.finishunit = psave[0].finishunit + temp;
                        LastAdd = (ptemp.finishunit == psave[0].startunit) ? LastAdd : ptemp.finishunit;//这次操作结束如果还有空间则lastadd改变值

                        //设置单元链属性
                        utemp.colour = pnew.colour;
                        utemp.belongto = pnew.PName;
                        for (int i = ptemp.startunit; i < ptemp.finishunit; i++)
                        {
                            usave[i] = utemp;
                        }
                        //添加进程入链表并排序
                        psave.Add(ptemp);
                        psave.Sort((a, b) => a.startunit.CompareTo(b.startunit));
                        return true;
                    }
                }
                for (int i = 1; i < psave.Count; i++)
                {
                    if (psave[i].startunit - psave[i - 1].finishunit >= temp)//如果这两个进程之间单元足够
                    {
                        //设置本次节点属性
                        ptemp = pnew;
                        ptemp.startunit = psave[i-1].finishunit;
                        ptemp.finishunit = psave[i-1].finishunit + temp;
                        LastAdd = (ptemp.finishunit==psave[i].startunit)? LastAdd:ptemp.finishunit;//这次操作结束如果还有空间则lastadd改变值

                        //设置单元链属性
                        utemp.colour = pnew.colour;
                        utemp.belongto = pnew.PName;
                        for (int j = ptemp.startunit; j < ptemp.finishunit; j++)
                        {
                            usave[j] = utemp;
                        }
                        //添加进程入链表并排序
                        psave.Add(ptemp);
                        psave.Sort((a, b) => a.startunit.CompareTo(b.startunit));
                        return true;
                    }
                }
                if (UnitRealNum - psave[psave.Count - 1].finishunit >= temp)//如果最后一个进程内存单元末尾和最大内存单元数量大于等于需要的数量
                {
                    //设置本次节点属性
                    ptemp = pnew;
                    ptemp.startunit = psave[psave.Count - 1].finishunit;
                    ptemp.finishunit = psave[psave.Count - 1].finishunit + temp;
                    LastAdd = ptemp.finishunit;//设置lastadd为本次操作结尾

                    //设置单元链属性
                    utemp.colour = pnew.colour;
                    utemp.belongto = pnew.PName;
                    for (int j = ptemp.startunit; j < ptemp.finishunit; j++)
                    {
                        usave[j] = utemp;
                    }
                    //添加进程入链表(最后不需要排序)
                    psave.Add(ptemp);
                    return true;
                }
            }
            return false;
        }
        private bool NextFit(ProgramSave pnew)
        {           
            int temp = pnew.PMUse / Unit;//计算本次单元需求(小数点则+1)
            if (pnew.PMUse % Unit != 0)
                temp++;
            UnitSave utemp = new UnitSave();
            ProgramSave ptemp = new ProgramSave();

            if (psave.Count == 0)//空内存
            {
                if (pnew.PMUse > Memory)//空了都大于最大内存
                    return false;
                else//内存足够
                {
                    //设置本次节点属性
                    ptemp = pnew;
                    ptemp.startunit = 0;
                    ptemp.finishunit = temp;
                    LastAdd = temp;//设置lastadd为本次操作结尾

                    //设置单元链属性
                    utemp.colour = pnew.colour;
                    utemp.belongto = pnew.PName;
                    for (int i = ptemp.startunit; i < ptemp.finishunit; i++)
                    {
                        usave[i] = utemp;
                    }
                    //添加进程入链表(最后不需要排序)
                    psave.Add(ptemp);
                    return true;
                }
            }
            else if (psave.Count == 1)//只有一个进程
            {
                if (LastAdd >= psave[0].finishunit)//lastadd在进程末尾单元之后
                {
                    if (UnitRealNum - LastAdd < temp)//上次操作末尾单元和最大单元差值小于需求单元大小
                    {
                        int TLastAdd = 0;
                        if (psave[0].startunit - TLastAdd < temp)//从头开始到唯一进程开始单元空间都不够
                        {
                            if (psave[0].finishunit < LastAdd)//如果进程末尾单元在最后一次操作单元之前
                            {
                                if (UnitRealNum - psave[0].finishunit < temp)//如果进程末尾单元和末尾内存单元空间之间不够
                                    return false;
                                else
                                {
                                    TLastAdd = psave[0].finishunit;
                                    //设置本次节点属性
                                    ptemp = pnew;
                                    ptemp.startunit = TLastAdd;
                                    ptemp.finishunit = TLastAdd + temp;
                                    LastAdd = (ptemp.finishunit == psave[0].startunit) ? LastAdd : ptemp.finishunit;//这次操作结束如果还有空间则lastadd改变值

                                    //设置单元链属性
                                    utemp.colour = pnew.colour;
                                    utemp.belongto = pnew.PName;
                                    for (int i = ptemp.startunit; i < ptemp.finishunit; i++)
                                    {
                                        usave[i] = utemp;
                                    }
                                    //添加进程入链表排序
                                    psave.Add(ptemp);
                                    psave.Sort((a, b) => a.startunit.CompareTo(b.startunit));
                                    return true;
                                }
                            }
                        }

                        else
                        {
                            //设置本次节点属性
                            ptemp = pnew;
                            ptemp.startunit = TLastAdd;
                            ptemp.finishunit = TLastAdd + temp;
                            LastAdd = (ptemp.finishunit == psave[0].startunit) ? LastAdd : ptemp.finishunit;//这次操作结束如果还有空间则lastadd改变值

                            //设置单元链属性
                            utemp.colour = pnew.colour;
                            utemp.belongto = pnew.PName;
                            for (int i = ptemp.startunit; i < ptemp.finishunit; i++)
                            {
                                usave[i] = utemp;
                            }
                            //添加进程入链表排序
                            psave.Add(ptemp);
                            psave.Sort((a, b) => a.startunit.CompareTo(b.startunit));
                            return true;
                        }
                    }
                    else//大于的话
                    {
                        //设置本次节点属性
                        ptemp = pnew;
                        ptemp.startunit = LastAdd;
                        ptemp.finishunit = LastAdd + temp;
                        LastAdd = ptemp.finishunit;//设置lastadd为本次操作结尾

                        //设置单元链属性
                        utemp.colour = pnew.colour;
                        utemp.belongto = pnew.PName;
                        for (int i = ptemp.startunit; i < ptemp.finishunit; i++)
                        {
                            usave[i] = utemp;
                        }
                        //添加进程入链表(最后不需要排序)
                        psave.Add(ptemp);
                        return true;
                    }
                }
                else
                {
                    if(psave[0].startunit-LastAdd>=temp)//lastadd到进程开始前内存够
                    {
                        //设置本次节点属性
                        ptemp = pnew;
                        ptemp.startunit = LastAdd;
                        ptemp.finishunit = LastAdd + temp;
                        LastAdd = (ptemp.finishunit == psave[0].startunit) ? psave[0].finishunit:ptemp.finishunit;//这次操作结束如果还有空间则lastadd改变值

                        //设置单元链属性
                        utemp.colour = pnew.colour;
                        utemp.belongto = pnew.PName;
                        for (int i = ptemp.startunit; i < ptemp.finishunit; i++)
                        {
                            usave[i] = utemp;
                        }
                        //添加进程入链表排序
                        psave.Add(ptemp);
                        psave.Sort((a, b) => a.startunit.CompareTo(b.startunit));
                        return true;
                    }
                    else
                    {
                        LastAdd = psave[0].finishunit;
                        if (UnitRealNum - LastAdd < temp)//上次操作末尾单元和最大单元差值小于需求单元大小
                        {
                            int TLastAdd = 0;
                            if (psave[0].startunit - TLastAdd < temp)//从头开始到唯一进程开始单元空间都不够
                            {
                                if (psave[0].finishunit < LastAdd)//如果进程末尾单元在最后一次操作单元之前
                                {
                                    if (UnitRealNum - psave[0].finishunit < temp)//如果进程末尾单元和末尾内存单元空间之间不够
                                        return false;
                                    else
                                    {
                                        TLastAdd = psave[0].finishunit;
                                        //设置本次节点属性
                                        ptemp = pnew;
                                        ptemp.startunit = TLastAdd;
                                        ptemp.finishunit = TLastAdd + temp;
                                        LastAdd = (ptemp.finishunit == psave[0].startunit) ? LastAdd : ptemp.finishunit;//这次操作结束如果还有空间则lastadd改变值

                                        //设置单元链属性
                                        utemp.colour = pnew.colour;
                                        utemp.belongto = pnew.PName;
                                        for (int i = ptemp.startunit; i < ptemp.finishunit; i++)
                                        {
                                            usave[i] = utemp;
                                        }
                                        //添加进程入链表排序
                                        psave.Add(ptemp);
                                        psave.Sort((a, b) => a.startunit.CompareTo(b.startunit));
                                        return true;
                                    }
                                }
                            }

                            else
                            {
                                //设置本次节点属性
                                ptemp = pnew;
                                ptemp.startunit = TLastAdd;
                                ptemp.finishunit = TLastAdd + temp;
                                LastAdd = (ptemp.finishunit == psave[0].startunit) ? LastAdd : ptemp.finishunit;//这次操作结束如果还有空间则lastadd改变值

                                //设置单元链属性
                                utemp.colour = pnew.colour;
                                utemp.belongto = pnew.PName;
                                for (int i = ptemp.startunit; i < ptemp.finishunit; i++)
                                {
                                    usave[i] = utemp;
                                }
                                //添加进程入链表排序
                                psave.Add(ptemp);
                                psave.Sort((a, b) => a.startunit.CompareTo(b.startunit));
                                return true;
                            }
                        }
                        else//大于的话
                        {
                            //设置本次节点属性
                            ptemp = pnew;
                            ptemp.startunit = LastAdd;
                            ptemp.finishunit = LastAdd + temp;
                            LastAdd = ptemp.finishunit;//设置lastadd为本次操作结尾

                            //设置单元链属性
                            utemp.colour = pnew.colour;
                            utemp.belongto = pnew.PName;
                            for (int i = ptemp.startunit; i < ptemp.finishunit; i++)
                            {
                                usave[i] = utemp;
                            }
                            //添加进程入链表(最后不需要排序)
                            psave.Add(ptemp);
                            return true;
                        }
                    }
                }

            }
            else//大于一个进程情况
            {
               if(LastAdd>=psave[psave.Count-1].finishunit)//上次添加就是最后一个进程
                {
                    if (UnitRealNum - LastAdd < temp)//如果最后进程末尾单元和最大内存单元差值都不够
                    {
                        for(int i=0;i<psave.Count-1;i++)//从头进程开始查找
                        {
                            if (psave[i + 1].startunit > LastAdd)//如果查找的后一个进程的开始单元大于最后操作单元位置
                                return false;
                            else//小于的话
                            {
                                if(psave[i+1].startunit-psave[i].finishunit>=temp)//如果i+1进程开始单元减去i进程结束单元大于等于本次需求单元大小
                                {
                                    //设置本次节点属性
                                    ptemp = pnew;
                                    ptemp.startunit = psave[i].finishunit;
                                    ptemp.finishunit = psave[i].finishunit + temp;

                                    if(ptemp.finishunit == psave[i + 1].startunit)
                                    {
                                        i++;
                                        while (i < psave.Count-1 || psave[i + 1].startunit - psave[i].finishunit <= 0) { i++; }
                                        LastAdd = psave[i].finishunit;
                                    }
                                    else
                                    {
                                        LastAdd = ptemp.finishunit;
                                    }
                                   

                                    //设置单元链属性
                                    utemp.colour = pnew.colour;
                                    utemp.belongto = pnew.PName;
                                    for (int j = ptemp.startunit; j < ptemp.finishunit; j++)
                                    {
                                        usave[j] = utemp;
                                    }
                                    //添加进程入链表并对链表重新排序
                                    psave.Add(ptemp);
                                    psave.Sort((a, b) => a.startunit.CompareTo(b.startunit));

                                    
                                    return true;
                                }
                            }
                        }
                    }
                    else//如果足够
                    {
                        //设置本次节点属性
                        ptemp = pnew;
                        ptemp.startunit = LastAdd;
                        ptemp.finishunit = LastAdd + temp;
                        LastAdd = ptemp.finishunit;//设置lastadd为本次操作结尾

                        //设置单元链属性
                        utemp.colour = pnew.colour;
                        utemp.belongto = pnew.PName;
                        for (int j = ptemp.startunit; j < ptemp.finishunit; j++)
                        {
                            usave[j] = utemp;
                        }
                        //添加进程入链表(最后一个不需要排序)
                        psave.Add(ptemp);
                        return true;
                    }
                }
                else//如果是进程之间
                {
                    int j = psave.FindIndex(p => p.finishunit == LastAdd);
                    if (j == -1)//表示没找到进程但这个就是为空
                    {
                        j = psave.FindIndex(p => p.startunit > LastAdd);//找比lastAdd位置多一点的进程位置
                        if (psave[j].startunit - LastAdd >= temp)//这次就够了
                        {
                            //设置本次节点属性
                            ptemp = pnew;
                            ptemp.startunit = psave[j].finishunit;
                            ptemp.finishunit = psave[j].finishunit + temp;

                            if (ptemp.finishunit == psave[j + 1].startunit)
                            {
                                j++;
                                while (j < psave.Count || psave[j + 1].startunit - psave[j].finishunit <= 0) { j++; }
                                LastAdd = psave[j].finishunit;
                            }
                            else
                            {
                                LastAdd = ptemp.finishunit;
                            }


                            //设置单元链属性
                            utemp.colour = pnew.colour;
                            utemp.belongto = pnew.PName;
                            for (int m = ptemp.startunit; m < ptemp.finishunit; m++)
                            {
                                usave[m] = utemp;
                            }
                            //添加进程入链表并对链表重新排序
                            psave.Add(ptemp);
                            psave.Sort((a, b) => a.startunit.CompareTo(b.startunit));


                            return true;
                        }
                        else
                        {
                            for (; j < psave.Count - 1; j++)
                            {
                                if (psave[j + 1].startunit - psave[j].finishunit >= temp)//i+1进程开始单元减去i进程结束单元比需要的大或相等
                                {
                                    //设置本次节点属性
                                    ptemp = pnew;
                                    ptemp.startunit = psave[j].finishunit;
                                    ptemp.finishunit = psave[j].finishunit + temp;

                                    if (ptemp.finishunit == psave[j + 1].startunit)
                                    {
                                        j++;
                                        while (j < psave.Count || psave[j + 1].startunit - psave[j].finishunit <= 0) { j++; }
                                        LastAdd = psave[j].finishunit;
                                    }
                                    else
                                    {
                                        LastAdd = ptemp.finishunit;
                                    }

                                    //设置单元链属性
                                    utemp.colour = pnew.colour;
                                    utemp.belongto = pnew.PName;
                                    for (int m = ptemp.startunit; m < ptemp.finishunit; m++)
                                    {
                                        usave[m] = utemp;
                                    }
                                    //添加进程入链表并排序
                                    psave.Add(ptemp);
                                    psave.Sort((a, b) => a.startunit.CompareTo(b.startunit));
                                    return true;
                                }
                            }
                            //如果一直到结束都没找到足够大的
                            LastAdd = psave[psave.Count - 1].finishunit;//从末尾开始找
                            if (UnitRealNum - LastAdd < temp)//如果最后进程末尾单元和最大内存单元差值都不够
                            {
                                for (int i = 0; i < psave.Count - 1; i++)//从头进程开始查找
                                {
                                    if (psave[i + 1].startunit > LastAdd)//如果查找的后一个进程的开始单元大于最后操作单元位置
                                        return false;
                                    else//小于的话
                                    {
                                        if (psave[i + 1].startunit - psave[i].finishunit >= temp)//如果i+1进程开始单元减去i进程结束单元大于等于本次需求单元大小
                                        {
                                            //设置本次节点属性
                                            ptemp = pnew;
                                            ptemp.startunit = psave[i].finishunit;
                                            ptemp.finishunit = psave[i].finishunit + temp;

                                            if (ptemp.finishunit == psave[i + 1].startunit)
                                            {
                                                i++;
                                                while (i < psave.Count || psave[i + 1].startunit - psave[i].finishunit <= 0) { i++; }
                                                LastAdd = psave[i].finishunit;
                                            }
                                            else
                                            {
                                                LastAdd = ptemp.finishunit;
                                            }


                                            //设置单元链属性
                                            utemp.colour = pnew.colour;
                                            utemp.belongto = pnew.PName;
                                            for (int m = ptemp.startunit; m < ptemp.finishunit; m++)
                                            {
                                                usave[m] = utemp;
                                            }
                                            //添加进程入链表并对链表重新排序
                                            psave.Add(ptemp);
                                            psave.Sort((a, b) => a.startunit.CompareTo(b.startunit));


                                            return true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        for (; j< psave.Count - 1; j++)
                        {
                            if (psave[j + 1].startunit - psave[j].finishunit >= temp)//i+1进程开始单元减去i进程结束单元比需要的大或相等
                            {
                                //设置本次节点属性
                                ptemp = pnew;
                                ptemp.startunit = psave[j].finishunit;
                                ptemp.finishunit = psave[j].finishunit + temp;

                                if (ptemp.finishunit == psave[j + 1].startunit)
                                {
                                    j++;
                                    while (j < psave.Count || psave[j + 1].startunit - psave[j].finishunit <= 0) {j++; }
                                    LastAdd = psave[j].finishunit;
                                }
                                else
                                {
                                    LastAdd = ptemp.finishunit;
                                }

                                //设置单元链属性
                                utemp.colour = pnew.colour;
                                utemp.belongto = pnew.PName;
                                for (int m = ptemp.startunit; m < ptemp.finishunit; m++)
                                {
                                    usave[m] = utemp;
                                }
                                //添加进程入链表并排序
                                psave.Add(ptemp);
                                psave.Sort((a, b) => a.startunit.CompareTo(b.startunit));
                                return true;
                            }
                        }
                        //如果一直到结束都没找到足够大的
                        LastAdd = psave[psave.Count - 1].finishunit;//从末尾开始找
                        if (UnitRealNum - LastAdd < temp)//如果最后进程末尾单元和最大内存单元差值都不够
                        {
                            for (int i = 0; i < psave.Count - 1; i++)//从头进程开始查找
                            {
                                if (psave[i + 1].startunit > LastAdd)//如果查找的后一个进程的开始单元大于最后操作单元位置
                                    return false;
                                else//小于的话
                                {
                                    if (psave[i + 1].startunit - psave[i].finishunit >= temp)//如果i+1进程开始单元减去i进程结束单元大于等于本次需求单元大小
                                    {
                                        //设置本次节点属性
                                        ptemp = pnew;
                                        ptemp.startunit = psave[i].finishunit;
                                        ptemp.finishunit = psave[i].finishunit + temp;

                                        if (ptemp.finishunit == psave[i + 1].startunit)
                                        {
                                            i++;
                                            while (i < psave.Count || psave[i + 1].startunit - psave[i].finishunit <= 0) { i++; }
                                            LastAdd = psave[i].finishunit;
                                        }
                                        else
                                        {
                                            LastAdd = ptemp.finishunit;
                                        }


                                        //设置单元链属性
                                        utemp.colour = pnew.colour;
                                        utemp.belongto = pnew.PName;
                                        for (int m = ptemp.startunit; m < ptemp.finishunit; m++)
                                        {
                                            usave[m] = utemp;
                                        }
                                        //添加进程入链表并对链表重新排序
                                        psave.Add(ptemp);
                                        psave.Sort((a, b) => a.startunit.CompareTo(b.startunit));


                                        return true;
                                    }
                                }
                            }
                        }
                        else//如果足够
                        {
                            //设置本次节点属性
                            ptemp = pnew;
                            ptemp.startunit = LastAdd;
                            ptemp.finishunit = psave[psave.Count - 1].finishunit + temp;
                            LastAdd = ptemp.finishunit;//设置lastadd为本次操作结尾

                            //设置单元链属性
                            utemp.colour = pnew.colour;
                            utemp.belongto = pnew.PName;
                            for (int m = ptemp.startunit; m < ptemp.finishunit; m++)
                            {
                                usave[m] = utemp;
                            }
                            //添加进程入链表(最后一个不需要排序)
                            psave.Add(ptemp);
                            return true;
                        }
                    }
                }             
            }
            return false;
        }

        //来不及实现存储链操作
        /*private bool BestFit(ProgramSave pnew)
        {

            return false;
        }
        private bool WorstFit(ProgramSave pnew)
        {

            return false;
        }*/

        //对内存单元块操作
        public String getunitcolor(int count)
        {
            if (count > usave.Count)
            {
                MessageBox.Show("读取溢出!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return "Black";
            }
            else
            {
                return (usave[count].colour);
            }
        }
        public String getunitbelong(int count)
        {
            {
                if (count > usave.Count-1)
                {
                    return "Unknown";
                }
                else
                {
                    return (usave[count].belongto);
                }
            }
        }
    }
}
